<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('h'))
{
    function h($str)
    {
        //return nl2br(str_replace(" ", "&nbsp;", htmlspecialchars($str)));
		return nl2br(htmlspecialchars($str));
    }
}

if ( ! function_exists('n'))
{
    function n($str)
    {
        return number_format($str, 2, '.', ',');
    }
}

if ( ! function_exists('n2'))
{
    function n2($str)
    {
		if(empty($str)) $str = 0;
		if(is_int($str*1)) return "$str";
        return number_format($str, 2, '.', ',');
    }
}

if ( ! function_exists('sort_column'))
{
	function sort_column($title, $sort_field, $sort_type, $base_url, $cur_sort_field='', $filter_k='',$function =''){
        $base_url = trim($base_url, '/').'/'.$sort_field;   
               
        if($cur_sort_field == $sort_field){
            $classname = ($sort_type==1)?'sort_column_desc':'sort_column_asc';
			if($sort_type == 1)
				$base_url .= '/0';
			else
				$base_url .= '/1';
        }else{			
            $classname = 'sort_column';
			$base_url .= '/0';
        }
		if ($filter_k!='') $base_url .= '/'.h($filter_k);
        $s = '<nobr><a href="'.site_url($base_url).'" class="'.$classname.'">'.$title.'</a></nobr>';
        
        if($function)
         $s = '<nobr><a href="javascript:'.$function.'(\''.$base_url.'\')" class="'.$classname.'">'.$title.'</a></nobr>';
     
        return $s;
    }
}

if ( ! function_exists('use_lightbox'))
{
    function use_lightbox()
    {
        $s = '<script type="text/javascript" src="'.base_url().'application/assets/scripts/ibox.js"></script>
              <script type="text/javascript">
                iBox.setPath(\''.base_url().'application/assets/styles/\');
                iBox.fade_in_speed = 20;
              </script>
              <link rel="stylesheet" href="'.base_url().'application/assets/styles/lightbox.css" type="text/css" media="screen"/>';
        return $s;
    }
}
if ( ! function_exists('show_wrap_text'))
{
    function show_wrap_text($str, $n = 15, $end_char = '...')
    {
        $shortname = wrap_text($str, $n).(strlen($str)<=$n?'':$end_char);
        return '<a title="'.h($str).'" alt="'.h($str).'"><nobr>'.h($shortname).'</nobr></a>';
    }
}
if ( ! function_exists('wrap_text'))
{
    function wrap_text($str, $n = 18)
    {
        if(strlen($str)<=$n)
            return $str;
        return substr($str, 0, $n);
    }
}
if ( ! function_exists('view_content'))
{	
	function view_content_vbb($text, $tags = array('ATTACH'))
		{			
			$text = preg_replace("/\[ATTACH=CONFIG[^\]]*\](.*)\[\/ATTACH\]/iU",'',$text);
			return $text;
		}		
	function view_content($text, $tags = array('img','p'))
		{			
			$args = func_get_args();
			$text = array_shift($args);
			$tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;
			foreach ($tags as $tag){
				if(preg_match_all('/<'.$tag.'[^>]*>(.*)<\/'.$tag.'>/iU', $text, $found)){
					$text = str_replace($found[0],$found[1],$text);
			  }
			}	
			return $text;
		}		
}

function getBrowser()
{
    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $bname = 'Unknown';
    $platform = 'Unknown';
    $version= "";

    //First get the platform?
    if (preg_match('/linux/i', $u_agent)) {
        $platform = 'linux';
    }
    elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
        $platform = 'mac';
    }
    elseif (preg_match('/windows|win32/i', $u_agent)) {
        $platform = 'windows';
    }
   
    // Next get the name of the useragent yes seperately and for good reason
    if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
    {
        $bname = 'Internet Explorer';
        $ub = "MSIE";
    }
    elseif(preg_match('/Firefox/i',$u_agent))
    {
        $bname = 'Mozilla Firefox';
        $ub = "Firefox";
    }
    elseif(preg_match('/Chrome/i',$u_agent))
    {
        $bname = 'Google Chrome';
        $ub = "Chrome";
    }
    elseif(preg_match('/Safari/i',$u_agent))
    {
        $bname = 'Apple Safari';
        $ub = "Safari";
    }
    elseif(preg_match('/Opera/i',$u_agent))
    {
        $bname = 'Opera';
        $ub = "Opera";
    }
    elseif(preg_match('/Netscape/i',$u_agent))
    {
        $bname = 'Netscape';
        $ub = "Netscape";
    }
   
    // finally get the correct version number
    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .
    ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
        // we have no matching number just continue
    }
   
    // see how many we have
    $i = count($matches['browser']);
    if ($i != 1) {
        //we will have two since we are not using 'other' argument yet
        //see if version is before or after the name
        if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
            $version= $matches['version'][0];
        }
        else {
            $version= $matches['version'][1];
        }
    }
    else {
        $version= $matches['version'][0];
    }
   
    // check if we have a number
    if ($version==null || $version=="") {$version="?";}
   
    return array(
        'userAgent' => $u_agent,
        'name'      => $bname,
        'version'   => $version,
        'platform'  => $platform,
        'pattern'    => $pattern
    );
} 
function browser(){
    $agent = $_SERVER['HTTP_USER_AGENT'];

    // initialize properties
    $bd['platform'] = "Unknown";
    $bd['browser'] = "Unknown";
    $bd['version'] = "Unknown";

    // find operating system
    if (eregi("win", $agent))
        $bd['platform'] = "Windows";
    elseif (eregi("mac", $agent))
        $bd['platform'] = "MacIntosh";
    elseif (eregi("linux", $agent))
        $bd['platform'] = "Linux";
    elseif (eregi("OS/2", $agent))
        $bd['platform'] = "OS/2";
    elseif (eregi("BeOS", $agent))
        $bd['platform'] = "BeOS";

    // test for Opera        
    if (eregi("opera",$agent)){
        $val = stristr($agent, "opera");
        if (eregi("/", $val)){
            $val = explode("/",$val);
            $bd['browser'] = $val[0];
            $val = explode(" ",$val[1]);
            $bd['version'] = $val[0];
        }else{
            $val = explode(" ",stristr($val,"opera"));
            $bd['browser'] = $val[0];
            $bd['version'] = $val[1];
        }

    // test for WebTV
    }elseif(eregi("webtv",$agent)){
        $val = explode("/",stristr($agent,"webtv"));
        $bd['browser'] = $val[0];
        $bd['version'] = $val[1];
    
    // test for MS Internet Explorer version 1
    }elseif(eregi("microsoft internet explorer", $agent)){
        $bd['browser'] = "MSIE";
        $bd['version'] = "1.0";
        $var = stristr($agent, "/");
        if (ereg("308|425|426|474|0b1", $var)){
            $bd['version'] = "1.5";
        }

    // test for NetPositive
    }elseif(eregi("NetPositive", $agent)){
        $val = explode("/",stristr($agent,"NetPositive"));
        $bd['platform'] = "BeOS";
        $bd['browser'] = $val[0];
        $bd['version'] = $val[1];

    // test for MS Internet Explorer
    }elseif(eregi("msie",$agent) && !eregi("opera",$agent)){
        $val = explode(" ",stristr($agent,"msie"));
        $bd['browser'] = $val[0];
        $bd['version'] = $val[1];
    
    // test for MS Pocket Internet Explorer
    }elseif(eregi("mspie",$agent) || eregi('pocket', $agent)){
        $val = explode(" ",stristr($agent,"mspie"));
        $bd['browser'] = "MSPIE";
        $bd['platform'] = "WindowsCE";
        if (eregi("mspie", $agent))
            $bd['version'] = $val[1];
        else {
            $val = explode("/",$agent);
            $bd['version'] = $val[1];
        }
        
    // test for Galeon
    }elseif(eregi("galeon",$agent)){
        $val = explode(" ",stristr($agent,"galeon"));
        $val = explode("/",$val[0]);
        $bd['browser'] = $val[0];
        $bd['version'] = $val[1];
        
    // test for Konqueror
    }elseif(eregi("Konqueror",$agent)){
        $val = explode(" ",stristr($agent,"Konqueror"));
        $val = explode("/",$val[0]);
        $bd['browser'] = $val[0];
        $bd['version'] = $val[1];
        
    // test for iCab
    }elseif(eregi("icab",$agent)){
        $val = explode(" ",stristr($agent,"icab"));
        $bd['browser'] = $val[0];
        $bd['version'] = $val[1];

    // test for OmniWeb
    }elseif(eregi("omniweb",$agent)){
        $val = explode("/",stristr($agent,"omniweb"));
        $bd['browser'] = $val[0];
        $bd['version'] = $val[1];

    // test for Phoenix
    }elseif(eregi("Phoenix", $agent)){
        $bd['browser'] = "Phoenix";
        $val = explode("/", stristr($agent,"Phoenix/"));
        $bd['version'] = $val[1];
    
    // test for Firebird
    }elseif(eregi("firebird", $agent)){
        $bd['browser']="Firebird";
        $val = stristr($agent, "Firebird");
        $val = explode("/",$val);
        $bd['version'] = $val[1];
        
    // test for Firefox
    }elseif(eregi("Firefox", $agent)){
        $bd['browser']="Firefox";
        $val = stristr($agent, "Firefox");
        $val = explode("/",$val);
        $bd['version'] = $val[1];
        
  // test for Mozilla Alpha/Beta Versions
    }elseif(eregi("mozilla",$agent) && 
        eregi("rv:[0-9].[0-9][a-b]",$agent) && !eregi("netscape",$agent)){
        $bd['browser'] = "Mozilla";
        $val = explode(" ",stristr($agent,"rv:"));
        eregi("rv:[0-9].[0-9][a-b]",$agent,$val);
        $bd['version'] = str_replace("rv:","",$val[0]);
        
    // test for Mozilla Stable Versions
    }elseif(eregi("mozilla",$agent) &&
        eregi("rv:[0-9]\.[0-9]",$agent) && !eregi("netscape",$agent)){
        $bd['browser'] = "Mozilla";
        $val = explode(" ",stristr($agent,"rv:"));
        eregi("rv:[0-9]\.[0-9]\.[0-9]",$agent,$val);
        $bd['version'] = str_replace("rv:","",$val[0]);
    
    // test for Lynx & Amaya
    }elseif(eregi("libwww", $agent)){
        if (eregi("amaya", $agent)){
            $val = explode("/",stristr($agent,"amaya"));
            $bd['browser'] = "Amaya";
            $val = explode(" ", $val[1]);
            $bd['version'] = $val[0];
        } else {
            $val = explode("/",$agent);
            $bd['browser'] = "Lynx";
            $bd['version'] = $val[1];
        }
    
    // test for Safari
    }elseif(eregi("safari", $agent)){
        $bd['browser'] = "Safari";
        $bd['version'] = "";

    // remaining two tests are for Netscape
    }elseif(eregi("netscape",$agent)){
        $val = explode(" ",stristr($agent,"netscape"));
        $val = explode("/",$val[0]);
        $bd['browser'] = $val[0];
        $bd['version'] = $val[1];
    }elseif(eregi("mozilla",$agent) && !eregi("rv:[0-9]\.[0-9]\.[0-9]",$agent)){
        $val = explode(" ",stristr($agent,"mozilla"));
        $val = explode("/",$val[0]);
        $bd['browser'] = "Netscape";
        $bd['version'] = $val[1];
    }
    
    // clean up extraneous garbage that may be in the name
    $bd['browser'] = ereg_replace("[^a-z,A-Z]", "", $bd['browser']);
    // clean up extraneous garbage that may be in the version        
    $bd['version'] = ereg_replace("[^0-9,.,a-z,A-Z]", "", $bd['version']);
    
    // check for AOL
    if (eregi("AOL", $agent)){
        $var = stristr($agent, "AOL");
        $var = explode(" ", $var);
        $bd['aol'] = ereg_replace("[^0-9,.,a-z,A-Z]", "", $var[1]);
    }
    return $bd;
}

function isFirefox(){
    /*$b = browser();
    return $b['browser'] == 'Firefox';*/
    //fix php 5.3
    $ua=getBrowser();
	return $ua['name'] == 'Mozilla Firefox';
}

function msg_no_permission(){
    $CI = &get_instance();
    $CI->load->module_language('administrator.users.user');
    $CI->load->helper('language');
    return lang('_user_no_permisson');
}